package mamikeji.liaowa.ui.activity.notice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import mamikeji.liaowa.R;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

public class Notice_ReplyActivity extends BaseActivity {

    @BindView(R.id.relativeLayout_notice_reply_fabulous)
    RelativeLayout relativeLayoutNoticeReplyFabulous;
    @BindView(R.id.relativeLayout_notice_reply_comment)
    RelativeLayout relativeLayoutNoticeReplyComment;
    @BindView(R.id.textView_notice_reply_fabulous)
    TextView textViewNoticeReplyFabulous;
    @BindView(R.id.textView_notice_reply_comment)
    TextView textViewNoticeReplyComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_reply);
        ButterKnife.bind(this);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.notice_reply_name);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        Map<String, String> formMap = new HashMap<>();
        formMap.put("pageindex", "1");
        formMap.put("pagesize", "10");
        formMap.put("usercode", ForPreference.getString(ForContents.USER_CODE));
        ForHttp.setMsg(HttpApi.ROOT_PATH + "api/Message/GetNotMessageCount", formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                System.out.println("未读消息:"+response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if ("200".equals(jsonObject.getString(HttpKey.STATUS))) {
                        JSONObject object = jsonObject.getJSONObject("data");
                        int praise = object.getInt("praise");
                        int comments = object.getInt("comments");

                        int num = 0;

                        if(praise>0){
                            ++num;
                            textViewNoticeReplyFabulous.setVisibility(View.VISIBLE);
                            textViewNoticeReplyFabulous.setText(praise+"");
                        }else{
                            textViewNoticeReplyFabulous.setVisibility(View.GONE);
                        }
                        if(comments>0){
                            ++num;
                            textViewNoticeReplyComment.setVisibility(View.VISIBLE);
                            textViewNoticeReplyComment.setText(comments+"");
                        }else{
                            textViewNoticeReplyComment.setVisibility(View.GONE);
                        }

                        ForPreference.putInt(ForContents.APP_NOTICE_MSG , num);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick({R.id.relativeLayout_notice_reply_fabulous, R.id.relativeLayout_notice_reply_comment})
    public void onViewClicked(View view) {
        Intent mIntent = null;
        switch (view.getId()) {
            case R.id.relativeLayout_notice_reply_fabulous:
                mIntent = new Intent(Notice_ReplyActivity.this, Notice_FabulousActivity.class);
                break;
            case R.id.relativeLayout_notice_reply_comment:
                mIntent = new Intent(Notice_ReplyActivity.this, Notice_CommentActivity.class);
                break;
        }
        if (mIntent != null) {
            startActivity(mIntent);
        }
    }

}
